/** Description of GeneralHelper.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.helpers;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.RemoteViews;

import com.panther.awp.android.smoker.R;
import com.panther.awp.android.smoker.widgets.SmallWidgetProvider;

public class GeneralHelper {
	
	public static final String SMALL_SMOKE_WIDGET_RECEIVER = "com.panther.awp.android.smoker.widgets.small.smokeNow";
	public static final String SMALL_WIDGET_SCROLLDOWN_RECEIVER = "com.panther.awp.android.smoker.widgets.small.scrolldown";
	public static final String BRAND_ID = "brandId";
	public static final String BRAND_PRICE = "brandPrice";
	public static final String BRAND_NAME = "brandName";
	
	public static void updateRemoteView(Context context, String returnIdStr, String returnBrand, String returnBrandPrice, Bitmap bitmapImage) {
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.smallsmokewidgetlayout);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		ComponentName thisWidget = new ComponentName(context, SmallWidgetProvider.class);
		
		Intent smokeNow = new Intent(context, SmallWidgetProvider.class);
		smokeNow.setAction(SMALL_SMOKE_WIDGET_RECEIVER);
		smokeNow.putExtra(BRAND_ID, returnIdStr);
		smokeNow.putExtra(BRAND_NAME, returnBrand);
		smokeNow.putExtra(BRAND_PRICE, returnBrandPrice);
		
		Intent scrollDown = new Intent(context, SmallWidgetProvider.class);
		scrollDown.setAction(SMALL_WIDGET_SCROLLDOWN_RECEIVER);
		scrollDown.putExtra(BRAND_ID, returnIdStr);
		
		remoteViews.setTextViewText(R.id.smallWidgetBrandId, returnIdStr);
		remoteViews.setTextViewText(R.id.smallWidgetBrandDisplay, returnBrand);
		remoteViews.setTextViewText(R.id.smallWidgetBrandPrice, returnBrandPrice);
		if(bitmapImage != null) {
			remoteViews.setImageViewBitmap(R.id.smokeIconSmallWidget, bitmapImage);
		} else {
			remoteViews.setImageViewResource(R.id.smokeIconSmallWidget, R.drawable.icon);
		}
		
		PendingIntent smokePendingIntent = PendingIntent.getBroadcast(context, 0, smokeNow, PendingIntent.FLAG_UPDATE_CURRENT);
		PendingIntent scrollDownPendingIntent = PendingIntent.getBroadcast(context, 0, scrollDown, PendingIntent.FLAG_UPDATE_CURRENT);

		remoteViews.setOnClickPendingIntent(R.id.smallWidgetSmokeButton, smokePendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.smallWidgetBrandScrollDown, scrollDownPendingIntent);
		remoteViews.setOnClickPendingIntent(R.id.smallWidgetBrandScrollDown_burn, scrollDownPendingIntent);
		
		appWidgetManager.updateAppWidget(thisWidget, remoteViews);
	}
}
